package com.spring.campus_express_delivery.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spring.campus_express_delivery.dao.OrdersDao;
import com.spring.campus_express_delivery.entity.ExpressType;
import com.spring.campus_express_delivery.entity.Orders;
import com.spring.campus_express_delivery.entity.PageBean;
import com.spring.campus_express_delivery.entity.Users;
import com.spring.campus_express_delivery.service.OrdersService;
import com.spring.campus_express_delivery.service.UsersService;
import com.spring.campus_express_delivery.util.MyUtil;
import com.spring.campus_express_delivery.util.ResponseEntity;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrdersServiceImpl implements OrdersService {
    @Resource
    OrdersDao ordersDao;
    @Override
    public void insertOrder(Orders orders){
        ordersDao.insertOrder(orders);
    }
    @Override
    public Orders selectId(Integer id){
        return ordersDao.selectId(id);
    }
    //修改订单状态（接单）
    @Override
    public void updateOrderStatus(Orders orders){
        ordersDao.updateOrderStatus(orders);
    }
    //修改订单状态（确认收货）
    @Override
    public void updateOrderStatusByF(Orders orders){
        ordersDao.updateOrderStatusByF(orders);
    }
    //修改订单状态（取消接单）
    @Override
    public void updateOrderStatusByCancel(Orders orders){
        ordersDao.updateOrderStatusByCancel(orders);
    }
    @Override
    //修改订单
    public  void updateOrders(Orders orders){
        ordersDao.updateOrders(orders);
    }

    //快递重量类型列表
    @Override
    public PageBean<Orders> selectOrderWeightByPage(int size, int page, String sort, String asc) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);

        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);

        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrderWeightByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    @Override
    public int selectCountByOrders() {
        return ordersDao.selectCountByOrders();
    }


    //根据快递编号查询订单编号
    @Override
    public Orders selectOrderNumByExpressNum(String expressNum){
        return ordersDao.selectOrderNumByExpressNum(expressNum);
}

    @Override
    //根据订单状态查询订单
    public Orders selectOrderStatus(String order_status){
        return ordersDao.selectOrderStatus(order_status);
    }
    //个人订单列表
    @Override
    public PageBean<Orders> selectOrdersSelfByPage(String identity_id,int size, int page, String sort, String asc) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);

        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);
        map.put("identity_id",identity_id);

        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrdersSelfByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //订单列表
    @Override
    public PageBean<Orders> selectOrdersByPage(int size, int page, String sort, String asc) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);

        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);

        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrdersByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //用户接的订单列表
    @Override
    public PageBean<Orders> selectBySender(String identity_id,int size, int page, String sort, String asc) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);

        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);
        map.put("identity_id",identity_id);

        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectBySender(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //删除订单
    public void deleteOrderById(Integer id){
        ordersDao.deleteOrderById(id);
    }

    //根据快递重量类型查询订单
    @Override
    public PageBean<Orders> selectOrderByExpressWeightTypeByPage(int size, int page, String sort, String asc,String expressWeightType) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);
        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);
        map.put("expressWeightType",expressWeightType);
        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrderByExpressWeightTypeByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //根据快递公司类型查询订单
    @Override
    public PageBean<Orders> selectOrderByExpressCompanyTypeByPage(int size, int page, String sort, String asc,String expressCompanyType) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);
        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);
        map.put("expressCompanyType",expressCompanyType);
        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrderByExpressCompanyTypeByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //根据送达地址查询订单
    @Override
    public PageBean<Orders> selectOrderByServiceAddressByPage(int size, int page, String sort, String asc,String serviceAddress) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);
        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);
        map.put("serviceAddress",serviceAddress);
        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrderByServiceAddressByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //根据订单状态查询订单
    @Override
    public PageBean<Orders> selectOrderByOrderStatusByPage(int size, int page, String sort, String asc,String order_status,String identity_id) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Orders> pageBean = new PageBean<Orders>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);
        //封装总记录数
        int totalCount = ordersDao.selectCountByOrders();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);
        map.put("order_status",order_status);
        map.put("identity_id",identity_id);
        //封装每页显示的数据
        List<Orders> lists = ordersDao.selectOrderByOrderStatusByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }
    //根据订单id修改快递进度
    @Override
    public void updateMiddle_address(Orders orders){
        ordersDao.updateMiddle_address(orders);
    }
    //统计数目
    @Override
    public Map<String, Integer> Orders_count(){
        Map<String,Integer> map = new HashMap<>();
        int countByOrders=ordersDao.selectCountByOrders();
        map.put("orders",countByOrders);
        return map;
    }
//    //导入订单信息
//    @Override
//    public void importOrdersExcel(MultipartFile usersFile) throws IOException {
//        Workbook workbook = null;
//        String fileName = usersFile.getOriginalFilename();
//        if (fileName.endsWith("xls")) {
//            workbook = new HSSFWorkbook(usersFile.getInputStream());
//        } else if (fileName.endsWith("xlsx")) {
//            workbook = new XSSFWorkbook(usersFile.getInputStream());
//        } else {
//            throw new IOException("文件不是Excel类型");
//        }
//        Sheet sheet = workbook.getSheet("sheet1");
//        int rows = sheet.getLastRowNum();
//        if (rows == 0) {
//            throw new IOException("没有数据导入失败");
//        }
//        for (int i = 1; i < rows + 1; i++) {
//            Row row = sheet.getRow(i);
//            if (row != null) {
//                Orders orders = new Orders();
//                MyUtil myUtil = new MyUtil();
//                String remark = myUtil.getCellValue(row.getCell(0));
//                Double orderType_price = Double.valueOf(myUtil.getCellValue(row.getCell(1)));
//                String user_name = myUtil.getCellValue(row.getCell(2));
//                String phone = myUtil.getCellValue(row.getCell(3));
//                String pick_code = myUtil.getCellValue(row.getCell(4));
//                String expressWeightType = myUtil.getCellValue(row.getCell(5));
//                String expressCompanyType = myUtil.getCellValue(row.getCell(6));
//                String serviceAddress = myUtil.getCellValue(row.getCell(7));
//                String whether_delete = myUtil.getCellValue(row.getCell(8));
//                orders.setRemark(remark);
//                orders.setOrderType_price(orderType_price);
//                orders.setUser_name(user_name);
//                orders.setPhone(phone);
//                orders.setPick_code(pick_code);
//                orders.setExpressWeightType(expressWeightType);
//                orders.setExpressCompanyType(expressCompanyType);
//                orders.setServiceAddress(serviceAddress);
//                orders.setWhether_delete(whether_delete);
//                ordersDao.insertOrder(orders);
//            }
//        }
//    }

    @Override
    public PageInfo<Orders> selectAllOrders(Integer pageNo,Integer pageSize) {
//        return ordersDao.selectAllOrders();
        PageHelper.startPage(pageNo, pageSize);
        List<Orders> list = ordersDao.selectAllOrders();
        PageInfo<Orders> ordersPageInfo = new PageInfo<>(list);
        return ordersPageInfo;
    }

    @Override
    public void exportOrdersExcel(HttpServletResponse response) throws IOException {

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCell cell = row.createCell(0);
        cell.setCellValue("id");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(1);//创建第二列
        cell.setCellValue("用户identity_id");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(2);//创建第二列
        cell.setCellValue("订单编号");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(3);//创建第二列
        cell.setCellValue("备注");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(4);//创建第二列
        cell.setCellValue("订单状态");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(5);//创建第二列
        cell.setCellValue("订单价格");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(6);//创建第二列
        cell.setCellValue("用户名");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(7);//创建第二列
        cell.setCellValue("联系方式");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(8);//创建第二列
        cell.setCellValue("接单员identity_id");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(9);//创建第二列
        cell.setCellValue("接单时间");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(10);//创建第二列
        cell.setCellValue("订单过期时间");//设置他的内容懂
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(11);//创建第二列
        cell.setCellValue("订单签收时间");//设置他的内容懂
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(12);//创建第二列
        cell.setCellValue("取件码");//设置他的内容懂
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(13);//创建第二列
        cell.setCellValue("快递重量类型");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(14);//创建第二列
        cell.setCellValue("快递取件地址");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(15);//创建第二列
        cell.setCellValue("快递送达地址");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(16);//创建第二列
        cell.setCellValue("快递中间地址");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(17);//创建第二列
        cell.setCellValue("是否删除");//设置他的内容
        cell.setCellStyle(style);//设置样式
//        cell = row.createCell(2);
//        cell.setCellValue("分数");
//        cell.setCellStyle(style);



        List<Orders> list = ordersDao.selectAllOrders();
        for (int i = 0; i < list.size() ; i++) {
            row = sheet.createRow(i + 1);
            Orders orders = list.get(i);
            row.createCell(0).setCellValue(orders.getId());
            row.createCell(1).setCellValue(orders.getUser_identity_id());
            row.createCell(2).setCellValue(orders.getOrderNum());
            row.createCell(3).setCellValue(orders.getRemark());
            row.createCell(4).setCellValue(orders.getOrder_status());
            if (orders.getOrderType_price() == null) {
                row.createCell(5).setCellValue(" ");
            } else {
                row.createCell(5).setCellValue(orders.getOrderType_price());
            }
            row.createCell(6).setCellValue(orders.getUser_name());
            row.createCell(7).setCellValue(orders.getPhone());
            row.createCell(8).setCellValue(orders.getSender_id());
            row.createCell(9).setCellValue(orders.getOrder_time());
            row.createCell(10).setCellValue(orders.getEnd_time());
            row.createCell(11).setCellValue(orders.getSign_time());
            row.createCell(12).setCellValue(orders.getPick_code());
            row.createCell(13).setCellValue(orders.getExpressWeightType());
            row.createCell(14).setCellValue(orders.getExpressCompanyType());
            row.createCell(15).setCellValue(orders.getServiceAddress());
            row.createCell(16).setCellValue(orders.getMiddle_address());
            row.createCell(17).setCellValue(orders.getWhether_delete());
//            row.createCell(2).setCellValue(stu.getScore());
//            cell = row.createCell(18);

        }

            OutputStream output = response.getOutputStream();
            //response.reset();
            long filename = System.currentTimeMillis();
            SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");
            String fileName = df.format(new Date());
            response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
            response.setContentType("application/msexcel");
//            response.reset();
//            response.addHeader("Access-Control-Allow-Origin","*");
//            response.addHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE");
//            response.addHeader("Access-Control-Allow-Headers","Content-Type");
            wb.write(output);

    }

    @Override
    public List<Orders> selectOrderByPick_code(String keyWord) {
        return ordersDao.selectOrderByPick_code(keyWord);
    }


}
